package com.weitu.hiddenDanger.controller;

import com.weitu.accessory.bean.AccessoryPackageInfo;
import com.weitu.accessory.service.AccessoryUtilService;
import com.weitu.accessory.util.AccessoryUtil;
import com.weitu.base.controller.BaseController;
import com.weitu.hiddenDanger.entity.HiddenDangerInfo;
import com.weitu.hiddenDanger.service.HDAcceptService;
import com.weitu.paging.DataGridPagination;
import com.weitu.plugin.PageView;
import com.weitu.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.weitu.util.AjaxResult.fail;
import static com.weitu.util.AjaxResult.success;

/**
 * 隐患验收Controller
 * Created by fanyuting on 2017/4/18 0018.
 */
@Controller
@RequestMapping("hiddenDanger/hdAccept")
public class HDAcceptController extends BaseController {
    private static final String MODULE_BATH_PATH = BASE_JSP_PATH + "hiddenDanger/hdAccept/";
    @Autowired
    private HDAcceptService hdAcceptService;
    @Autowired
    private AccessoryUtilService accessoryUtilService;//附件

    /**
     * 跳转到隐患治理列表页面
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public String toHDAcceptListPage(Model model) {

        return MODULE_BATH_PATH + "list";
    }

    /**
     * 查询分页列表
     *
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping("queryListByParam")
    public Object queryListByParam(HttpServletRequest request, HttpServletResponse response) {

        Map<String, Object> map = new HashMap<String, Object>();
        //获取前台参数
        putParametersIntoMap(map);
        Integer totalNum = hdAcceptService.queryListCount(map);//获取总数
        DataGridPagination pagination = DataGridPagination.getInstance(request, totalNum);
        PageView pageView = pagination.getPageView();//生成pageView分页对象
        map.put("pageView", pageView);
        List<HiddenDangerInfo> list = hdAcceptService.queryList(map);
        return pagination.flushDataGridData(list);
    }

    /**
     * 跳转到隐患验收页面
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("toHDAcceptPage")
    public String toHDAcceptPage(Model model, HttpServletRequest request) {
        String businessId = request.getParameter("businessId");
        Map<String, Object> map = new HashMap();
        map.put("id", businessId);
        List<HiddenDangerInfo> hdInfos = hdAcceptService.queryHDInfoById(map);
        model.addAttribute("hdInfo", hdInfos.get(0));
        return MODULE_BATH_PATH + "accept";
    }

    /**
     * 跳转到隐患验收详情页面
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("toHDAcceptDetailPage")
    public String toHDAcceptDetailPage(Model model, HttpServletRequest request) {
        String businessId = request.getParameter("businessId");
        Map<String, Object> map = new HashMap();
        map.put("id", businessId);
        map.put("hdId", businessId);
        List<HiddenDangerInfo> hdInfos = hdAcceptService.queryHDInfoById(map);
        Map<String, Object> acceptInfo = hdAcceptService.queryAcceptInfoByHDId(map);
        //查询该业务对应的附件信息
        List<AccessoryPackageInfo> accessoryList = accessoryUtilService.queryAccessoryByBusinessId(acceptInfo.get("id").toString());
        model.addAttribute("hdInfo", hdInfos.get(0));
        model.addAttribute("acceptInfo", acceptInfo);
        model.addAttribute("accessoryList", accessoryList);
        return MODULE_BATH_PATH + "detail";
    }

    /**
     * 隐患验收
     *
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping("accept")
    @Transactional
    public AjaxResult accept(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap();
        getAddBasicFieldMap(map);
        boolean accFlag = true;
        boolean flag = this.hdAcceptService.addHDAcceptInfo(map);
        if (flag) {
            accFlag = AccessoryUtil.saveAccerssoryInfo(map.get("id").toString());
        }
        if (accFlag) {
            return success("隐患验收登记成功！");
        }
        return fail("隐患验收登记失败！");
    }
}
